home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / timidsrc.zip / raw_a.c < prev    next >
Text File  |  1997-01-16  |  3KB  |  155 lines

  1. /* 
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     raw_audio.c
  21.  
  22.     Functions to output raw sound data to a file or stdout.
  23.  
  24. */
  25.  
  26. #ifdef __EMX__
  27. #include <stdlib.h>
  28. #endif
  29.  
  30. #ifdef __WIN32__
  31. #include <stdlib.h>
  32. #include <io.h>
  33. #include <string.h>
  34. #else
  35. #include <unistd.h>
  36. #endif
  37. #include <fcntl.h>
  38. #include <errno.h>
  39.  
  40. #ifdef __FreeBSD__
  41. #include <stdio.h>
  42. #endif
  43.  
  44. #include "config.h"
  45. #include "output.h"
  46. #include "controls.h"
  47.  
  48. #ifdef __WIN32__
  49. #define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
  50. #else
  51. #define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC
  52. #endif
  53.  
  54. static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
  55. static void close_output(void);
  56. static void output_data(int32 *buf, int32 count);
  57. static void flush_output(void);
  58. static void purge_output(void);
  59.  
  60. /* export the playback mode */
  61.  
  62.  
  63. #define dpm raw_play_mode
  64.  
  65. PlayMode dpm = {
  66.   DEFAULT_RATE, PE_16BIT|PE_SIGNED,
  67.   -1,
  68.   {0,0,0,0,0},
  69.   "raw waveform data", 'r',
  70.   "output.raw",
  71.   open_output,
  72.   close_output,
  73.   output_data,
  74.   flush_output,
  75.   purge_output  
  76. };
  77.  
  78. /*************************************************************************/
  79.  
  80. static int open_output(void)
  81. {
  82.   if (dpm.encoding & PE_ULAW)
  83.     dpm.encoding &= ~PE_16BIT;
  84.  
  85.   if (!(dpm.encoding & PE_16BIT))
  86.     dpm.encoding &= ~PE_BYTESWAP;
  87.  
  88.   if (dpm.name && dpm.name[0]=='-' && dpm.name[1]=='\0')
  89.     dpm.fd=1; /* data to stdout */
  90.   else
  91.     {
  92.       /* Open the audio file */
  93.         dpm.fd=open(dpm.name, OPEN_MODE, 0644);
  94.       if (dpm.fd<0)
  95.     {
  96.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
  97.             dpm.name, sys_errlist[errno]);
  98.       return -1;
  99.     }
  100.     }
  101.   return 0;
  102. }
  103.  
  104. static void output_data(int32 *buf, int32 count)
  105. {
  106.   if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
  107.   
  108.   if (dpm.encoding & PE_16BIT)
  109.     {
  110.       if (dpm.encoding & PE_BYTESWAP)
  111.     {
  112.       if (dpm.encoding & PE_SIGNED)
  113.         s32tos16x(buf, count);
  114.       else
  115.         s32tou16x(buf, count);
  116.     }
  117.       else
  118.     {
  119.       if (dpm.encoding & PE_SIGNED)
  120.         s32tos16(buf, count);
  121.       else 
  122.         s32tou16(buf, count);
  123.     }
  124.       
  125.       while ((-1==write(dpm.fd, buf, count * 2)) && errno==EINTR)
  126.     ;
  127.     }
  128.   else
  129.     {
  130.       if (dpm.encoding & PE_ULAW)
  131.     {
  132.       s32toulaw(buf, count);
  133.     }
  134.       else
  135.     {
  136.       if (dpm.encoding & PE_SIGNED)
  137.         s32tos8(buf, count);
  138.       else 
  139.         s32tou8(buf, count);
  140.     }
  141.       
  142.       while ((-1==write(dpm.fd, buf, count)) && errno==EINTR)
  143.     ;
  144.     }
  145. }
  146.  
  147. static void close_output(void)
  148. {
  149.   if (dpm.fd != 1) /* We don't close stdout */
  150.     close(dpm.fd);
  151. }
  152.  
  153. static void flush_output(void) { }
  154. static void purge_output(void) { }
  155.